<template>
  <div class="login-container">
    <!--
      如何使用一个之前没使用过的组件库
      1、找到组件的官网文档
      2、找到对应的版本vue2.X
      3、找时间全文了解一下当前组件库中提供了哪些组件
      4、根据ui设计图和需求文档确定当前页面需要哪些组件
      5、在组件文档中找到组件找到组件的示例
      6、再mian。js中全局引入或者再main.js中按需引入或者再当前文件中按需引入
      7、粘贴组件的示例（dom， data， methods， css）
      8、根据实际需要修改组件（添加或者删除属性、方法、指令、dom）
      9、预览
     -->
    <el-form
      ref="loginRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <!-- <div class="title-container">
        <h3 class="title">
          <img src="" alt="">
        </h3>
      </div> -->
      <el-form-item prop="loginName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.loginName"
          placeholder="请输入账号"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>
      <el-form-item prop="code">
        <span class="svg-container">
          <svg-icon icon-class="code" />
        </span>
        <el-input
          ref="code"
          v-model="loginForm.code"
          placeholder="请输入验证码"
          name="code"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="testImg" @click="TestImgFn">
          <img ref="imgRef" :src="`http://likede2-admin.itheima.net/likede/api/user-service/user/imageCode/${loginForm.clientToken}`">
        </span>
      </el-form-item>

      <el-button
        class="loginBtn"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        :loading="loading"
        @click.native.prevent="handleLogin"
      >登录</el-button>

      <!-- <div class="tips">
        <span style="margin-right: 20px">账号: 13800000002</span>
        <span> 密码: 123456</span>
      </div> -->
    </el-form>
  </div>
</template>

<script>
// import { login } from '@/api'
// import { testPhotoApi } from '@/api'
export default {
  name: 'Login',
  data() {
    return {
      loading: false,
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        loginType: 0,
        clientToken: '1234'
      },
      loginRules: {
        mobile: [
          { required: true, message: '请输入账号' }
        ],
        password: [
          { required: true, message: '请输入账号' }
        ],
        code: [
          { required: true, message: '请输入账号' }
        ]
      },
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async handleLogin() {
      this.loading = true
      try {
        await this.$store.dispatch('user/fetchLogin', this.loginForm)
        this.$router.replace('/')
        this.$message.success('登录成功')
        this.loading = false
      } catch (err) {
        this.TestImgFn()
        this.loading = false
        console.log(err)
      }
    },
    TestImgFn() {
      // console.log(e.target)
      this.$refs.imgRef.src = `http://likede2-admin.itheima.net/likede/api/user-service/user/imageCode/${this.loginForm.clientToken}`
    }
  }
}
</script>
<style lang="scss">
// 全局生效的样式
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #68b0fe;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: black;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .el-form-item {
    border: 2px solid rgb(102, 102, 102);
    // background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
    border-radius: 5px;
    color: #454545;
  }

  .el-form-item__error {
    color: #fff;
  }
}
</style>

<style lang="scss" scoped>
// 只对当前组件生效的样式
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url("~@/assets/common/login.jpg"); // 设置背景图片
  background-position: center; // 将图片位置设置为充满整个屏幕
  background-repeat: no-repeat;
  background-size: 100% 100%;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 80px 35px 0;
    margin: 350px auto;
    border-radius: 10px;
    overflow: hidden;
    background: #fff;
  }
  .loginBtn {
    background: #407ffe;
    height: 64px;
    line-height: 32px;
    font-size: 24px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.testImg{
  position:absolute;
  left: 69.9%;
   height: 100%;
}
.testImg img{
   height: 100%;
}
</style>
